﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class CustomerArchives_AddCustomerPreference : System.Web.UI.Page
{
    private DataTable PreferenceDetailTable
    {
        get
        {
            if (ViewState["PreferenceDetailTable"] == null)
            {
                ViewState["PreferenceDetailTable"] = CreatePreferenceDetailTable();
            }
            return (DataTable)ViewState["PreferenceDetailTable"];
        }
        set
        {
            ViewState["PreferenceDetailTable"] = value;
        }
    }
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    private DataTable CreatePreferenceDetailTable()
    {
        DataTable tbl = new DataTable();

        DataColumn tblColumn = new DataColumn();
        tblColumn.DataType = System.Type.GetType("System.String");
        tblColumn.ColumnName = "Preference_DetailName";
        tbl.Columns.Add(tblColumn);

        tblColumn = new DataColumn();
        tblColumn.DataType = System.Type.GetType("System.String");
        tblColumn.ColumnName = "Preference_DetailDescription";
        tbl.Columns.Add(tblColumn);        

        return tbl;
    }

    private void FillValueIntoPreferenceDetailTable(DataTable tbl)
    {
        Label lblDetailName;
        Label lblDetailDescription;

        for (int i = 0; i < dlistPreferenceDetail.Items.Count; i++)
        {
            lblDetailName = (Label)dlistPreferenceDetail.Items[i].FindControl("lblDetailName");
            lblDetailDescription = (Label)dlistPreferenceDetail.Items[i].FindControl("lblDetailDescription");

            tbl.Rows[i]["Preference_DetailName"] = lblDetailName.Text;
            tbl.Rows[i]["Preference_DetailDescription"] = lblDetailDescription.Text;
        }
    }

    public DataTable AddDataRowToDataTable(DataTable tbl, string detailName, string detailDescription)
    {
        DataRow newRow = tbl.NewRow();
        newRow["Preference_DetailName"] = detailName;
        newRow["Preference_DetailDescription"] = detailDescription;
        tbl.Rows.Add(newRow);
        return tbl;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("200500102");
        if (!IsPostBack)
        {
            
        }
    }
    protected void btnAddPreferenceDetail_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("200500102", "00001"))
        {
            Share.Alert(Page, "无权操作"); return;
        }
        if (txtPreferenceDetailName.Text == "")
        {
            Share.Alert(this.Page, "请填写喜好明细名称！"); return;
        }
        FillValueIntoPreferenceDetailTable(this.PreferenceDetailTable);
        this.PreferenceDetailTable = AddDataRowToDataTable(this.PreferenceDetailTable, txtPreferenceDetailName.Text, txtPreferenceDetailDescription.Text);
        dlistPreferenceDetail.DataSource = this.PreferenceDetailTable;
        dlistPreferenceDetail.DataBind();
        txtPreferenceDetailName.Text = "";
        txtPreferenceDetailDescription.Text = "";
    }
    protected void dlistPreferenceDetail_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (!Check.CheckPermission("200500102", "00003"))
        {
            Share.Alert(Page, "无权操作"); return;
        }
        if (e.CommandName.ToLower() == "del")
        {
            this.PreferenceDetailTable.Rows.RemoveAt(e.Item.ItemIndex);
        }
        dlistPreferenceDetail.DataSource = this.PreferenceDetailTable;
        dlistPreferenceDetail.DataBind();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("200500102", "00002"))
        {
            Share.Alert(Page, "无权操作"); return;
        }
        if (txtPreferenceMainName.Text == "")
        {
            Share.Alert(this.Page, "请填写喜好名称！"); return;
        }
        if (this.PreferenceDetailTable.Rows.Count == 0)
        {
            Share.Alert(this.Page, "请添加喜好明细！"); return;
        }

        string newPreferencesId = new BLL.CustomerPreferenceMain().GetNewPreferenceId();
        BLL.Common.ADDLSH("200500102");

        StringBuilder sbSql = new StringBuilder();
        sbSql.Append("INSERT INTO [CustomerPreferenceMain]([Preference_ID],[Preference_Name],[Preference_Description]) VALUES('");
        sbSql.Append(newPreferencesId);
        sbSql.Append("','");
        sbSql.Append(txtPreferenceMainName.Text);
        sbSql.Append("','");
        sbSql.Append(txtPreferenceMainDescription.Text);
        sbSql.Append("')|");

        for (int i = 0; i < this.PreferenceDetailTable.Rows.Count; i++)
        {
            sbSql.Append("INSERT INTO [CustomerPreferenceDetail]([Preference_MainID],[Preference_DetailName],[Preference_DetailDescription]) VALUES('");
            sbSql.Append(newPreferencesId);
            sbSql.Append("','");
            sbSql.Append(this.PreferenceDetailTable.Rows[i]["Preference_DetailName"].ToString());
            sbSql.Append("','");
            sbSql.Append(this.PreferenceDetailTable.Rows[i]["Preference_DetailDescription"].ToString());
            sbSql.Append("')|");
   
        }
        string stringSql = sbSql.ToString();
        char[] charArray = new char[] { '|' };
        string[] sqlCommandArray = stringSql.TrimEnd(charArray).Split(charArray);
        if (new SQLServerDAL.ORDERDOC().Deliver(sqlCommandArray))
        {
            Share.InsertLog("200500102", "00002");
            Response.Write("<script>alert('新增喜好成功！');window.location.href='CustomerPreferenceDetail.aspx?code=" + newPreferencesId + "'</script>");
        }
        else
        {
            Share.Alert(this.Page, "新增喜好失败！");
        }

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {

    }
}
